package com.spsy.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spsy.common.core.utils.poi.ExcelUtil;
import com.spsy.common.core.web.controller.BaseController;
import com.spsy.common.core.web.domain.AjaxResult;
import com.spsy.common.core.web.page.TableDataInfo;
import com.spsy.common.log.annotation.Log;
import com.spsy.common.log.enums.BusinessType;
import com.spsy.common.security.annotation.RequiresPermissions;
import com.spsy.product.domain.Product;
import com.spsy.product.domain.ProductDetail;
import com.spsy.product.domain.vo.ProductVO;
import com.spsy.product.service.IProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController
{

    @Resource
    private IProductService productService;

    @PostMapping("/add")
    public ProductVO addProduct(@RequestBody ProductAddForm form) {
        return productService.addProduct(form.getProduct(), form.getDetail());
    }

    @PostMapping("/update")
    public ProductVO updateProduct(@RequestBody ProductUpdateForm form) {
        return productService.updateProduct(form.getProduct(), form.getDetail());
    }

    @DeleteMapping("/{id}")
    public boolean deleteProduct(@PathVariable Long id) {
        return productService.deleteProduct(id);
    }

    @PostMapping("/{id}")
    public ProductVO getProduct(@PathVariable Long id) {
        return productService.getProductFromRedis(id);
    }

    @GetMapping("/page")
    public Page<ProductVO> getProductPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return productService.getProductPageFromRedis(pageNum, pageSize);
    }

    @GetMapping("/top5/price-desc")
    public List<ProductVO> getTop5ByPriceDesc() {
        return productService.getTop5ByPriceDescFromRedis();
    }

    // ---------- 接收参数的 Form 类（简化代码，实际可根据需求用 @RequestBody 直接接 Product/ProductDetail） ----------
    static class ProductAddForm {
        private Product product;
        private ProductDetail detail;

        // getters + setters
        public Product getProduct() { return product; }
        public void setProduct(Product product) { this.product = product; }
        public ProductDetail getDetail() { return detail; }
        public void setDetail(ProductDetail detail) { this.detail = detail; }
    }

    static class ProductUpdateForm {
        private Product product;
        private ProductDetail detail;

        // getters + setters
        public Product getProduct() { return product; }
        public void setProduct(Product product) { this.product = product; }
        public ProductDetail getDetail() { return detail; }
        public void setDetail(ProductDetail detail) { this.detail = detail; }
    }


    /**
     * 查询商品列表
     */
    @RequiresPermissions("product:product:list")
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("product:product:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product)
    {
        List<Product> list = productService.selectProductList(product);
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @RequiresPermissions("product:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }

    /**
     * 新增商品
     */
    @RequiresPermissions("product:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Product product)
    {
        return toAjax(productService.insertProduct(product));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("product:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Product product)
    {
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("product:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productService.deleteProductByIds(ids));
    }
}
